import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/utils/common_req_utils.dart';
import 'package:yuan_hang_app/widget/w_text.dart';
import '../../../../base/base_state.dart';
import '../../../../http/api.dart';
import '../../../../http/http_utils.dart';
import '../../../../res/w_constant.dart';
import '../../../../utils/common_utils.dart';
import '../../../../utils/string_utils.dart';
import '../../../../widget/label_item.dart';
import '../../../../widget/w_toast.dart';
import '../../model/book_manage_model.dart';

class BookDetailPage extends StatefulWidget {
  int bookId;
  String cancelPerson;
  String saleMan;
  BookDetailPage({super.key, required this.bookId,required this
  .cancelPerson, required this.saleMan});

  @override
  BaseState<BookDetailPage> createState() => _BookDetailPageState();
}

class _BookDetailPageState extends BaseState<BookDetailPage> {
  final _formKey = GlobalKey<FormState>();
  List<Map> modelList =
      CommonReqUtils.getLabelListByKey(CommonReqUtils.carType);
  List<Map> appointType =
      CommonReqUtils.getLabelListByKey(CommonReqUtils.appointType);
  List<Map> saleManList = WConstant().salesmanList;

  //初始化试驾预约登记对象
  Map bookInfo = {};

  GlobalKey<LabelItemState>? cancelReasonKey;
  GlobalKey<LabelItemState>? remarkKey;

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "预约信息查看",
        resizeToBottomInset: true,
        child: SingleChildScrollView(
            padding: EdgeInsets.zero, child: _bookDriveItem()));
  }

  Widget _bookDriveItem() {
    return Form(
        key: _formKey,
        child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(
                left: 16.px, right: 16.px, bottom: 10.px, top: 10.px),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(10.px),
                color: Colors.white,
                boxShadow: const [
                  BoxShadow(
                      //线框阴影线条
                      color: WColors.CDFEFFB,
                      offset: Offset(0, 1),
                      blurRadius: 3)
                ]),
            child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  LabelItem(
                    label: "客户名称",
                    value: bookInfo["customerName"],

                  ),
                  LabelItem(
                    value: bookInfo["phone"],
                    label: "手机号码",
                  ),
                  LabelItem(
                      label: "预约类型",
                      value: CommonReqUtils.exchangeCodeToName2(
                          appointType, StringUtils.safeGetValue(bookInfo["appointType"])) ??
                          "-"
                  ),
                  LabelItem(
                      label: "车型",
                      value: CommonReqUtils.exchangeCodeToName2(
                          modelList, StringUtils.safeGetValue(bookInfo["modelId"]))??
                          "-"
                  ),
                  LabelItem(
                    label: "接待顾问",
                      value: StringUtils.safeGetValue(widget.saleMan==""?StringUtils.safeGetValue(bookInfo["saleMan"], placeHolder: "-"):widget.saleMan)
                  ),
                  LabelItem(
                    label: "到店日期",
                    value: StringUtils.safeGetValue(bookInfo["arriveDate"]),
                  ),
                  LabelItem(
                    label: "到店时间",
                    value: StringUtils.safeGetValue(bookInfo["appointTime"]),
                  ),
                  LabelItem(
                    label: "备注",
                    key: remarkKey,
                    labelType: LabelType.TEXTAREA,
                    maxTextLength: 500,
                    isEnabled: false,
                    value: StringUtils.safeGetValue(bookInfo["remark"],
                        placeHolder: "-"),
                  ),
                  LabelItem(
                    label: "创建预约时间",
                    value: StringUtils.safeGetValue(bookInfo["createdAt"],
                        placeHolder: "-"),
                  ),
                  LabelItem(
                    label: "确认到店时间",
                    value: StringUtils.safeGetValue(
                        bookInfo["certainArriveTime"],
                        placeHolder: "-"),
                  ),
                  LabelItem(
                    label: "取消人",
                    value: StringUtils.safeGetValue(widget.cancelPerson,
                        placeHolder: "-"),
                  ),

                  Offstage(
                    offstage:bookInfo["appointStatus"] != 10211003 ,
                    child:   LabelItem(
                      label: "取消时间",
                      value: StringUtils.safeGetValue(bookInfo["cancelTime"],
                          placeHolder: "-"),
                    ),
                  ),
                  LabelItem(
                    key: cancelReasonKey,
                    labelType: LabelType.TEXTAREA,
                    label: "取消原因",
                    isLast: true,
                    isEnabled: false,
                    value: StringUtils.safeGetValue(bookInfo["cancelReason"],
                        placeHolder: "-"),
                    maxTextLength: 500,
                  ),
                ])));
  }

  Map<String, GlobalKey<LabelItemState>?> keyMap = {};

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    cancelReasonKey = GlobalKey(debugLabel: "cancelReason");

    remarkKey = GlobalKey(debugLabel: "remark");

    showLoading();
    //调用查询明细接口
    HttpUtils()
        .get("${Api.getTestDriveBookingDetail}/${widget.bookId}")
        .then((data) {
      hideLoading();
      if (data.resultCode == 200) {
        bookInfo = data.data;

        bookInfo["arriveDate"]=StringUtils.safeGetValue(bookInfo["arriveDate"]).substring(0,11);

        setState(() {});

        ///输入框类型
        cancelReasonKey!.currentState
            ?.refreshInput(bookInfo["cancelReason"] ?? "");
        remarkKey!.currentState?.refreshInput(bookInfo["remark"] ?? "");
      } else {
        Toast.toast(data.errMsg ?? "");
      }
    }).catchError((e) {
      hideLoading();
    });
  }
}
